import { defineStore } from 'pinia'
import http from '@/utils/httpRequest'

export const useCommonStore = defineStore('common', {
  state: () => ({
    documentClientHeight: 0,
    navbarLayoutType: 'default',
    sidebarLayoutSkin: 'dark',
    sidebarFold: false,
    menuList: [],
    menuActiveName: '',
    mainTabs: [],
    mainTabsActiveName: '',
    pathParm: {},
    topMenu: '',
    pagesRules: {
      getMenuIds: [],
      rules: []
    },
    cwrRceivedJeDisplay: '',
    fileSize: '',
    unreadMsgFlag: '',
    tableSel: {},
    dynamicMenuRoutes: []
  }),
  actions: {
    // 原mutations方法转换
    updateDocumentClientHeight(height) {
      this.documentClientHeight = height
    },
    updateNavbarLayoutType(type) {
      this.navbarLayoutType = type
    },
    updateSidebarLayoutSkin(skin) {
      this.sidebarLayoutSkin = skin
    },
    updateSidebarFold(fold) {
      this.sidebarFold = fold
    },
    updateMenuList(list) {
      this.menuList = list
    },
    updateMenuActiveName(name) {
      this.menuActiveName = name
    },
    updateMainTabs(tabs) {
      this.mainTabs = tabs
    },
    updateMainTabsActiveName(name) {
      this.mainTabsActiveName = name
    },
    updatePathParm(pathParm) {
      this.pathParm = pathParm
    },
    updateTopMenu(topMenu) {
      this.topMenu = topMenu
    },
    updatePagesRulesu(pagesRules) {
      console.log('设置规则', pagesRules)
      this.pagesRules = pagesRules
    },
    updateCwrRceivedJeDisplay(value) {
      this.cwrRceivedJeDisplay = value
    },
    updateFileSize(fileSize) {
      this.fileSize = fileSize
    },
    updateUnreadMsgFlag(str) {
      this.unreadMsgFlag = str
    },
    setTableSel(data) {
      if (!data) {
        this.tableSel = {}
        return
      }
      this.tableSel = { ...this.tableSel, ...data }
    },
    setDynamicMenuRoutes(data) {
      this.dynamicMenuRoutes = data
    },

    // // 原actions方法转换
    // async getPagesRules(data) {
    //   if (this.pagesRules.getMenuIds.includes(data.id)) return
      
    //   const res = await http({
    //     url: http.adornUrl('/ty/ty/getRulesFile'),
    //     method: 'get',
    //     params: http.adornParams(data.params, false)
    //   })

    //   if (res.data?.code === 0 && res.data.fileContent) {
    //     const list = JSON.parse(res.data.fileContent)
    //     const rulesData = structuredClone(this.pagesRules)
    //     rulesData.getMenuIds.push(data.id)
        
    //     list.forEach(item => {
    //       if (!rulesData.rules.some(el => el.name === item.name)) {
    //         rulesData.rules.push(item)
    //       }
    //     })
        
    //     this.updatePagesRulesu(rulesData)
    //   }
    // }
  }
})
